package com.project.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.template.entity.Comments;
import com.project.template.entity.Posts;
import com.project.template.mapper.CommentsMapper;
import com.project.template.service.CommentsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 功能：
 * 作者：lhp
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Override
    public Page<Comments> page(Map<String, Object> query, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Comments> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Comments::getId);
        //条件模糊查询
//        if(query.get("name")!=null){
//            wrapper.like(Posts::getId,query.get("name"));
//        }
        return page(new Page<>(pageNum,pageSize),wrapper);
    }
    @Resource
    private CommentsMapper commentsMapper;

    public List<Comments> getCommentsByPostIds(List<Integer> postIds) {
        if (postIds == null || postIds.isEmpty()) {
            return Collections.emptyList();
        }
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        wrapper.in("post_id", postIds).orderByAsc("comment_time");  // 使用in方法查找 post_id 在 postIds 列表中的数据
        return commentsMapper.selectList(wrapper);  // 查询并返回结果
    }
}
